<?php
class commonController extends Controller{
	
	public function __construct(){
		parent::__construct();
		$this->countAsk();
//		$this->userInfo();
		$this->topCate();
		$this->webconfig();
		$uid = isset($_GET['uid'])?(int)$_GET['uid']:'';
		$this->probability($uid);
		$this->userInfoSmarty();
		$this->searchUserInfo($uid);
		$this->userTop();
		$this->userTodayTop();
		$this->userHistoryTop();
	}
	/*
	 * 网站配置项
	 */
	 public function webconfig(){
	 	$this->assign('webName', C('WEBNAME'));
		$this->assign('KEYWORDS', C('KEYWORDS'));
		$this->assign('DISCRIPTION', C('DISCRIPTION'));
		$this->assign('COPY', C('COPY'));
		$this->assign('RECORD', C('RECORD'));
		$this->assign('RES_ON', C('RES_ON'));
		$this->assign('WEB_ON', C('WEB_ON'));
		 //奖励机制
		 $this->assign('answer', C('answer'));
		 $this->assign('accept', C('accept'));
		 $this->assign('answerdel', C('answerdel'));
		 $this->assign('askdel', C('askdel'));
		
		
	 }
	/*
	 * 获取顶级栏目
	 */
	public function topCate(){
		$topCate = M('wenda')->query('SELECT * FROM hd_category WHERE pid=0');
		$this->assign('topCate',$topCate);
	}
	/*
	 * 获取总数目
	 */
	public function countAsk(){
		$countAsk = M('hd_ask')->query('SELECT COUNT(asid) as n FROM hd_ask');
//		var_dump($countAsk);die;
		$this->assign('countAsk',$countAsk[0]['n']);
	}
	/*
	 * 方法调用方便查询用户信息  根据session 当前登录用户
	 */
	public function userInfo(){
//		var_dump($_SERVER);
//		echo $_SERVER['SERVER_ADDR'];
		if(!empty($_SESSION['uid'])){
			$uid = $_SESSION['uid'];
			$userInfo = M('hd_user')->query(" select * from hd_user where uid={$uid} ");
			return $userInfo;
			$userInfo = current($userInfo);
			$this->assign('userInfo',$userInfo);
		}else{
			$userInfo = '';
		}

	}
	/*
	 *查询页面模板可以调用用户信息 
	 */
	 public function userInfoSmarty(){
	 		if(is_array($this->userInfo())){
			 	$userInfoSmarty = $this->userInfo();
				$userInfoSmarty = current($userInfoSmarty);
				return $this->assign('userInfoSmarty',$userInfoSmarty);
			}
	 }
	/*
	 * 面包屑导航  
	 */
	public function navigation(){
		//获取栏目 子集分类
	 	$cid=(int)$_GET['cid'];
        //查询cid对应的pid
		$sonDate = M('hd_category')->query("SELECT * FROM hd_category WHERE pid={$cid}");
		//查询判断
		if(empty($sonDate)){
			$pid=M('hd_category')->query("SELECT * FROM hd_category WHERE cid={$cid} ");
			$pid=$pid[0]['pid'];
			$sonDate = M('category')->query("SELECT * FROM hd_category WHERE pid={$pid} ");
		}
		$this->assign('sonDate',$sonDate);
		//面包屑导航  
		$data = M('hd_category')->query("SELECT * FROM hd_category");//获得所有分类

		//忽的全部父级地柜处理
		$fatherData = $this->getFather($data,$cid);
		//数组翻转
		$fatherData = array_reverse($fatherData);
	
		$this->assign('fatherData',$fatherData);
		
		return 	$fatherData;
	}
	 /*
	  * 获得父级
	  */
	 private function getFather($data,$cid){
	 	$temp = array();
		foreach($data as $v){
			if($v['cid'] == $cid){
				$temp[] = $v;
				$temp = array_merge($temp,$this->getFather($data, $v['pid']));
			}
		}
		return $temp;
	 }
	 /*
	  * 发布验证
	  */
	 public function postcheck(){
	 	if(IS_POST){
	 		if(empty($_SESSION['uid'])){
				$this->error('请登录后提交');
			}	
			$uid = $_SESSION['uid']; //用户id
//			$asid = $_POST['asid']; //栏目id	
			$content =strip_tags($_POST['content']);
			$content = str_replace(array('\'','\"',',',';'),'',$content); 
//			var_dump($_POST);
	 		if(empty($_SESSION['username'])){
				$this->error('请登录后再提交');
			}
			if(empty($_POST['content'])){
				$this->error('内容部能为空');
			}
			
//			if(empty($_POST['cid']) || empty($_POST['asid'])){
//				$this->error('请确认提交路径是否合法，栏目必须选择');
//			}		
	 	}

			
	 }
	/*
	 * 查询指定用户信息
	 */
	 public function searchUserInfo($uid){
	 if(empty($uid)){
 		if(isset($_GET['uid'])){
 			$uid = $_GET['uid'];
		}else if(isset($_SESSION['uid'])){
			$uid = $_SESSION['uid'];
		}
	}
	 $searchUserInfo=M('hd_user')->query("SELECT * FROM hd_user WHERE uid='{$uid}' ");
	 return  $searchUserInfo = current($searchUserInfo); 	
	 }

	 /*
	  * 计算才采纳率
	  */
	 public function probability($uid){
	 		if(empty($uid)){
		 		if(isset($_GET['uid'])){
		 			$uid = $_GET['uid'];
				}else if(isset($_SESSION['uid'])){
					$uid = $_SESSION['uid'];
				}
			}
			$uidUserinfo = M('hd_user')->query("SELECT * FROM hd_user WHERE uid='{$uid}' ");
//			var_dump($uidUserinfo);die;
			$uidUserinfo = current($uidUserinfo);
			$this->assign('uidUserinfo', $uidUserinfo);
				//计算采纳率
			if($uidUserinfo['answer'] == 0 || $uidUserinfo['accept']==0 ){
				$probability='0%';
				return  $this->assign('probability',$probability);
			}else{
				$probability = $uidUserinfo['accept']/$uidUserinfo['answer']*100;
				$probability =round($probability,2).'%';
				return  $this->assign('probability',$probability);
			}
			
			
	 	}
	 /*
	  * 用户发帖回复排行
	  */
	 public function userTop(){
	 	$userTop = M('hd_user')->query("SELECT * FROM hd_user ORDER BY answer DESC  LIMIT 5");
		return $this->assign('userTop',$userTop);
	 }
	 /*
	  * 今天排行
	  */
	  public function userTodayTop(){
//	  	获得今天时间戳
	  	$todayTime = strtotime(date('Y-m-d'));
	  	$userTodayTop = M('hd_user')->query("select * from hd_user as user join hd_answer as answer on user.uid=answer.uid where answer.time>'{$todayTime}' group by user.answer  desc limit 1 ");
		$userTodayTop = current($userTodayTop);
    	$this->assign('userTodayTop',$userTodayTop);
		$userTodayTopUid = $userTodayTop['uid'];
//		采纳率
		$uidinfo =current(M('hd_user')->query("SELECT * FROM hd_user WHERE uid = '{$userTodayTopUid}' "));
		$uid = $uidinfo['uid'];
		if($uidinfo['answer'] == 0 || $uidinfo['accept']==0 ){
				$userTodayTopProbability='0%';
				return  $this->assign('userTodayTopProbability',$userTodayTopProbability);
			}else{
				$userTodayTopProbability = $uidinfo['accept']/$uidinfo['answer']*100;
				$userTodayTopProbability =round($userTodayTopProbability,2).'%';
				return  $this->assign('userTodayTopProbability',$userTodayTopProbability);
			}
	  }
	  /*
	   * 历史回答问题最多的人
	   */
	   public function userHistoryTop(){
	   	$userHistoryTop = M('hd_user')->query("SELECT * FROM hd_user ORDER BY answer DESC  LIMIT 1");
		$userHistoryTop = current($userHistoryTop);
		$this->assign('userHistoryTop',$userHistoryTop);
		$uid = $userHistoryTop['uid'];
		if($userHistoryTop['answer'] == 0 || $userHistoryTop['accept']==0 ){
				$userHistoryTopprobability='0%';
				return  $this->assign('userHistoryTopprobability',$userHistoryTopprobability);
			}else{
				$userHistoryTopprobability = $userHistoryTop['accept']/$userHistoryTop['answer']*100;
				$userHistoryTopprobability =round($userHistoryTopprobability,2).'%';
				return  $this->assign('userHistoryTopprobability',$userHistoryTopprobability);
			}
	   }
	
}	
